#pragma once


#include "OCMain/Environment.h"
#include "OCMain/DynLib.h"
#include "OCMain/DynLibManager.h"

using namespace OCPP;

Environment* mEnvironment = NULL;
DynLibManager* mDynLibManager = NULL;

#if WIN32
#include <Windows.h>

BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) {

    if (CTRL_CLOSE_EVENT == dwCtrlType) {
        CORE_SAFE_DELETE(mEnvironment);
        CORE_SAFE_DELETE(mDynLibManager);
    }

    return TRUE;
}
#endif

typedef void (*DLL_START_PLUGIN)(void);
typedef void (*DLL_STOP_PLUGIN)(void);

void init(String application)
{
    String baseName, appPath;
    StringUtil::splitFilename(application, baseName, appPath);
    mEnvironment = new Environment(appPath);
    mDynLibManager = new DynLibManager;
#if WIN32
    SetConsoleCtrlHandler(HandlerRoutine, true);
    StringVector loadPlugins = { "OCPP_FreeImageCodec"};
#else 
    StringVector loadPlugins = { "libOCPP_MyTest" };
#endif

    for (const auto& name : loadPlugins)
    {
        String pluginPath = appPath + name;
#if _DEBUG
        pluginPath += "d";
#endif
        if (DynLib* lib = Singleton(DynLibManager).load(pluginPath); lib != nullptr)
        {
            DLL_START_PLUGIN pFunc = (DLL_START_PLUGIN)lib->getSymbol("dllStartPlugin");
            if (pFunc)
            {
                pFunc();
            }
        }
    }
}